<?php 
    require_once ("Includes/session.php");
    require_once ("Includes/simplecms-config.php"); 
    require_once ("Includes/connectDB.php");
    confirm_is_admin();
    include("Includes/header.php"); 

        if (isset($_GET['submit_edit']))
        {
            $firstname = $_GET['firstname'];
            $lastname = $_GET['lastname'];

            // currently only supports full first and last name
            // TODO: nickname, id
            if ($firstname == NULL || $lastname == NULL)
                $error = "Error: Need first name and last name";

            $query = 'SELECT swimmers.id, swimmers.firstname, swimmers.lastname, '.
                        'swimmers.nickname, teams.name FROM swimmers, teams '.
                        'WHERE swimmers.firstname = ? AND swimmers.lastname = ? '.
                        ' AND teams.id = swimmers.team_id LIMIT 1';

            $statement = $databaseConnection->prepare($query);
            $statement->bind_param('ss', $firstname, $lastname);
            $statement->execute();
            $statement->store_result();

            if ($statement->error)
                $error = 'Error: Database query failed - ' . $statement->error;

            $statement->bind_result($swimid, $found_firstname, $found_lastname, $nickname, $teamname);
            if (!$statement->fetch())
                $error = "Error: could not find swimmer";

            if (!isset($error))
            {
                $stage = 'found';
                $lastname = $found_lastname;
                $firstname = $found_firstname;
            }
        }

        else if (isset($_POST['submit_edit']))
        {
            $swimid = $_POST['swimid'];
            $nickname = $_POST['nickname'];

            // test nick name
            $query = 'SELECT swimmers.nickname FROM swimmers WHERE swimmers.nickname = ?';
            $statement = $databaseConnection->prepare($query);
            $statement->bind_param('s', $nickname);
            $statement->execute();
            $statement->store_result();

            if ($statement->fetch())
                $error = 'Error: nickname must be unique';
            else
            {
                    //mysqli_report(MYSQLI_REPORT_ALL);
                    $query = 'UPDATE swimmers SET nickname = ? WHERE swimmers.id = ?';
                    $statement = $databaseConnection->prepare($query);
                    $statement->bind_param('si', $nickname, $swimid);
                    $statement->execute();
                    $statement->store_result();

                    if ($statement->error)
                        $error = 'Error: Database query failed - ' . $statement->error;
            }

            if (isset($error))
            {
                $lastname = $_POST['lastname'];
                $firstname = $_POST['firstname'];
                $teamname = $_POST['teamname'];
                $stage = 'final';
            }
            else
            {
                $msg = $_POST['firstname'].' updated successfully';
                $firstname = "";
                $lastname = "";
            }
        }

        else
        {
            $firstname = "";
            $lastname = "";
        } 
?>
<div id="main">
    <h2>Edit Swimmer</h2>
        <?php
        // prints out error msg
        if (isset($error))
            echo '<div class="error">'.$error.'</div>';
        if (isset($msg))
            echo '<div class="msg">'.$msg.'</div>';
        if (isset($stage)): ?>
            <form action="editswimmer.php" method="post">
        <?php else: ?>
            <form action="editswimmer.php" method="get">
        <?php endif; ?>
            <fieldset>
                <ol>
                    <li>
                        <label for="firstname">First Name:</label> 
                        <input type="text" name="firstname" value="<?php echo $firstname;?>" id="firstname" <?php if (isset($stage)) echo 'readonly="readonly"';?> />
                    </li>
                    <li>
                        <label for="content">Last Name:</label>
                        <input type="text" name="lastname" value="<?php echo $lastname;?>" id="lastname" <?php if (isset($stage)) echo 'readonly="readonly"';?>/>
                    </li>
                    <?php if (isset($stage)): ?>
                        <li>
                            <label for="content">Nick Name:</label>
                            <input type="text" name="nickname" value="<?php echo $nickname;?>" id="nickname" />
                        </li>
                        <li>
                            <label for="content">Team:</label>
                            <input type="text" name="teamname" value="<?php echo $teamname;?>" id="teamname" readonly="readonly"/>
                        </li>
                        <li>
                            <input type="hidden" name="swimid" value="<?php echo $swimid;?>"/>
                        </li>
                    <?php endif; ?>
                </ol>
                <legend>Edit Swimmer</legend>
                <input type="submit" name="submit_edit" value="Edit Swimmer" />
            </fieldset>
        </form>
        <p><a href="index.php">Cancel</a></p>
</div>
</div> <!-- End of outer-wrapper which opens in header.php -->
<?php include ("Includes/footer.php"); ?>